package main

import (
	"fmt"
	"strconv"
	"strings"
)

func getDigit(ptr *int, s string) int {
	beg := *ptr
	for *ptr < len(s) {
		if s[*ptr] >= '0' && s[*ptr] <= '9' {
			*ptr++
			continue
		}

		break
	}

	num, _ := strconv.Atoi(s[beg:*ptr])
	return num
}

func decodeString(s string) string {
	//result := ""
	ptr := 0
	numStack := make([]int, 0)
	strStack := make([]byte, 0)
	for ptr < len(s) {
		cur := s[ptr]
		if cur <= '9' && cur >= '0' {
			digit := getDigit(&ptr, s)
			numStack = append(numStack, digit)
		} else if (cur >= 'a' && cur <= 'z' || cur >= 'A' && cur <= 'Z') || cur == '[' {
			strStack = append(strStack, cur)
			ptr++
		} else {
			i := len(strStack) - 1
			for ; i >= 0; i-- {
				if strStack[i] == '[' {
					break
				}
			}
			repeat := strings.Repeat(string(strStack[i+1:]), numStack[len(numStack)-1])
			numStack = numStack[:len(numStack)-1]
			strStack = strStack[:i]
			strStack = append(strStack, []byte(repeat)...)
			ptr++
		}
	}
	return string(strStack)
}
func main() {
	fmt.Println(decodeString("3[a2[c]]"))

}
